    /* 
    * File:      GtkTextView演示 
    * Author:   blackboy,    blackboycpp@gmail.com 
    * D/T:       2010.9.29 
    */  
      
    #include <gtk/gtk.h>  
      
    int main (int argc, char *argv[])  
    {  
        GtkWidget* win;  
        GtkWidget* vbox;  
        GtkWidget* text_view;  
        GtkTextBuffer* buffer;  
        GtkTextIter start, end;  
        PangoFontDescription* font_desc;  
        GdkColor color;  
        GtkTextTag* tag;  
      
        gtk_init (&argc, &argv);  
      
        win = gtk_window_new (GTK_WINDOW_TOPLEVEL);  
        gtk_container_set_border_width (GTK_CONTAINER (win),  1);  
        gtk_window_set_title (GTK_WINDOW (win), "GtkTextView Demo");  
        gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);  
        gtk_window_set_default_size(GTK_WINDOW(win), 400,200);  
        g_signal_connect (win, "destroy", gtk_main_quit, NULL);  
      
        vbox = gtk_vbox_new(TRUE, 1);  
      
        text_view = gtk_text_view_new();  
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));  
        gtk_text_buffer_set_text(buffer, "Hello, this is  some text", -1);  
      
        /* 更改整个TextView的默认字体与颜色 */  
        font_desc = pango_font_description_from_string("Courier New 15");  
        gtk_widget_modify_font(text_view, font_desc);  
        pango_font_description_free(font_desc);  
        gdk_color_parse("red", &color);  
        gtk_widget_modify_text(text_view, GTK_STATE_NORMAL, &color);  
      
        /* 设置文本缩进 */  
        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_view), 30);  
        /* 利用tag更改一部分文本的背景色 */  
        tag = gtk_text_buffer_create_tag(buffer, "blue_foreground",  
                                         "background", "yellow", NULL);  
        gtk_text_buffer_get_iter_at_offset(buffer, &start, 7);  
        gtk_text_buffer_get_iter_at_offset(buffer, &end, 12);  
        gtk_text_buffer_apply_tag(buffer, tag, &start, &end);  
      
        gtk_box_pack_start(GTK_BOX(vbox), text_view, FALSE, TRUE, 0);  
        gtk_container_add(GTK_CONTAINER(win), vbox);  
      
        gtk_widget_show_all (win);  
        gtk_main ();  
        return 0;  
    }  
